function addReport() {
    var node = getSelectNode();

    $("#report-nd").val(node.nd);
    initReportType($("#report-nd").get(0),node.code);
    $("#edit-modal").modal({backdrop: 'static', keyboard: false});
}

function edit_modal_close() {
    $("#report-id").val("");
    $("#report-nd").get(0).selectedIndex = 0;
    $("#report-code").val("");
    $("#report-name").val("");
    $("#report-sequNo").val("");
    $("#report-nd").removeAttr("disabled");
    $("#report-code").removeAttr("disabled");
    $("#report-type").removeAttr("disabled");
    $("#warn-text").css("display", "none");
    $("#edit-modal").modal("hide");
}

function getSelectNode(){
    var ref = $('#jstree_rpttype').jstree(true);
    var sel = ref.get_selected();
    var nodeData= ref.get_node(sel);
    return nodeData.original;
}

function editReport() {
    selectRows = $('#reportlist').DataTable().rows('.selected');
    if ((selectRows.indexes().length ==0)||(selectRows.indexes().length >1)){
        $("#error-text").text("请选择一行编辑！");
        $("#error-modal").modal({backdrop: 'static', keyboard: false});
        return;
    };
    var selecteddata =selectRows.data()[0];

    $("#report-id").val(selecteddata.id);
    $("#report-nd").val(selecteddata.nd);
    initReportType($("#report-nd").get(0),selecteddata.reportType);
    $("#report-code").val(selecteddata.reportCode);
    $("#report-name").val(selecteddata.reportName);
    $("#report-areaCount").val(selecteddata.areaCount);
    $("#report-sequNo").val(selecteddata.sequNo);

    $("#report-nd").attr("disabled", "disabled");
    $("#report-code").attr("disabled", "disabled");
    $("#report-type").attr("disabled", "disabled");

    $("#edit-modal").modal({backdrop: 'static', keyboard: false});
}


delete_report_open = function () {
    selectRows = $('#reportlist').DataTable().rows('.selected');
    if (selectRows.indexes().length ==0){
        $("#error-text").text("请选择要删除的数据！");
        $("#error-modal").modal({backdrop: 'static', keyboard: false});
        return;
    };
    $("#delete-report-modal").modal({backdrop: 'static', keyboard: false});
}

delete_report_close = function () {
    $("#delete-report-modal").modal("hide");
}

function deleteReport() {
    selectRows = $('#reportlist').DataTable().rows('.selected');
    var idList=[];
    for (var i=0;i<selectRows.indexes().length;i++){
        idList[i] = selectRows.data()[i].id;
    }

    $.ajax({
        url: "/agcfs/report/deleteReports",
        type: "POST",
        dataType: "json",
        data: {ids: idList.join(',')},
        success: function (data) {
            refreshRptTable();
            $("#delete-report-modal").modal("hide");
            if (data.result == "success") {
                saveSuccess();
            } else {
                saveError(data);
            }
        }
    });
}

function initReportType(obj,value) {
    var nd = obj.value;
    var rptType = "#report-type";
    $.ajax({
        url: "/agcfs/report/getRptType",
        type: "GET",
        dataType: "json",
        data: {nd: nd},
        success: function (data) {
            $(rptType).css("color", "");
            $(rptType).html("");
            for (var i = 0; i < data.length; i++) {
                var temp = '<option value=' + data[i].code + '>' + data[i].text + '</option>';
                $(rptType).append(temp);
            }
            if ((typeof(value) != "undefined")&&(value!="")) {
                $(rptType).val(value);
            }
        }
    });
}

function saveReport() {
    var id = $("#report-id").val();
    var nd = $("#report-nd").val();
    var reportCode = $("#report-code").val();
    var reportName = $("#report-name").val();
    var reportType = $("#report-type").val();
    var forDwTypes = $("#report-dwtypes").val();
    var areaCount = $("#report-areaCount").val();
    var sequNo = $("#report-sequNo").val();
    $.ajax({
        url: "/agcfs/report/saveReport",
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify({
            id: id,
            nd: nd,
            reportCode: reportCode,
            reportName: reportName,
            reportType: reportType,
            reportTypeName: "",
            forDwTypes: forDwTypes,
            areaCount: areaCount,
            dispType: "0",
            sequNo: sequNo
        }),
        success: function (data) {
            sendAjaxSuccess(data);
        }
    });
}

function sendAjaxSuccess(data) {
    if (data.result == "success") {
        saveSuccess();
        refreshRptTable();
        edit_modal_close();
    } else {
        $("#edit-modal").modal("hide");
        saveError(data);
    }
}

function saveSuccess() {
    $("#save-success-new").css("display", "block");
    $("#save-success-new").fadeOut(3000);
}

function saveError(data){
    if(data.saveError != undefined){
        $("#error-text").text(data.saveError);
    } else {
        $("#error-text").text(data.reason);
    }
    $("#error-modal").modal({backdrop: 'static', keyboard: false});
}



